package TestDemo.example.controller;

import TestDemo.example.pojo.PageResult;
import TestDemo.example.pojo.Result;
import TestDemo.example.pojo.Student;
import TestDemo.example.pojo.StudentQueryParam;
import TestDemo.example.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;

@Slf4j
@RestController
@RequestMapping ("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @GetMapping
    public Result getStudentList(StudentQueryParam studentQueryParam) {
        log.info("getStudentList");
        PageResult pageResult = studentService.getStudentParamList(studentQueryParam);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{ids}")
    public Result deleteById(@PathVariable("ids") ArrayList<Integer> ids) {
        log.info("deleteById{}", ids);
        studentService.deleteById(ids);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("save{}", student);
        studentService.add(student);
        return Result.success();
    }

    @GetMapping ("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        log.info("getById{}", id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("update{}", student);
        studentService.updateStudent(student);
        return Result.success();
    }

    @PutMapping ("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable("id") Integer id, @PathVariable("score") Integer score) {
        log.info("updateViolation{}", id);
        studentService.updateViolation(id, score);
        return Result.success();
    }

}
